package app.GUI;


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import app.Data.User;
import app.Logic.*;


public class HomeScreen {
	
	private static final String TITLE = "School Scheduler+";
	private static JFrame frame;
	
	private static UserAccounts accounts;
	
	private JMenuBar menuBar;
	private JMenu fileMenu;
	private JMenuItem createAccount;
	private JMenuItem editAccount;
	private JMenuItem login;
	private JMenuItem logout;
	private JMenuItem exit;
	
	private JMenu editMenu;
	private JMenu settingsMenu;
	private JMenu helpMenu;
	
	private ExitHandler exitHandler;
	private LogInHandler loginHandler;
	private CreateAccountHandler createHandler;
	private LogOutHandler logoutHandler;
	
	public HomeScreen(){
		frame = new JFrame();
		frame.setLocation(0,0);
		
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        double h = screenSize.height/1.5;
        int height = (int) h;
        System.out.println("Height = "+height);
        double w = screenSize.width/1.5;
        int width = (int) w;
        System.out.println("Width = "+width);
		
		frame.setSize(width, height);
		
		frame.setResizable(false);
		frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		
		frame.setTitle(TITLE);
		frame.setLocationRelativeTo(null);
		
		accounts = new UserAccounts();
		accounts.loadAccounts();
	}
	
	public JFrame getFrame(){
		return frame;
	}
	
	public void displayMenuBar(){
		menuBar = new JMenuBar();
		
		//file menu data
		fileMenu = new JMenu("File");
		createAccount = new JMenuItem("Create Account");
		editAccount = new JMenuItem("Edit Account");
		login = new JMenuItem("Log In");
		logout = new JMenuItem("Log Out");
		exit = new JMenuItem("Exit");
		
		createHandler = new CreateAccountHandler();
		createAccount.addActionListener(createHandler);
		
		loginHandler = new LogInHandler();
		login.addActionListener(loginHandler);
		
		logoutHandler = new LogOutHandler();
		logout.addActionListener(logoutHandler);
		
		exitHandler = new ExitHandler();
		exit.addActionListener(exitHandler);
		
		fileMenu.add(createAccount);
		fileMenu.add(editAccount);
		fileMenu.add(login);
		fileMenu.add(logout);
		fileMenu.addSeparator();
		fileMenu.add(exit);
		
		//disable some default components
		editAccount.setEnabled(false);
		logout.setEnabled(false);
		
		menuBar.add(fileMenu);
		
		//edit menu data
		editMenu = new JMenu("Edit");
		menuBar.add(editMenu);
		
		//settings menu data
		settingsMenu = new JMenu("Settings");
		menuBar.add(settingsMenu);
		
		
		//help menu data
		helpMenu = new JMenu("Help");
		menuBar.add(helpMenu);
		
		//add to frame object
		frame.add(menuBar, BorderLayout.NORTH);
	}
	
	public void loginGUI(){
		login.setEnabled(false);
		logout.setEnabled(true);
		createAccount.setEnabled(false);
		editAccount.setEnabled(true);
	}
	
	public void logoutGUI(){
		login.setEnabled(true);
		logout.setEnabled(false);
		createAccount.setEnabled(true);
		editAccount.setEnabled(false);
	}
	
	
	public static void main(String [] args){
		HomeScreen screen = new HomeScreen();
		screen.displayMenuBar();
		frame.setVisible(true);
	}
	
	class ExitHandler extends WindowAdapter implements ActionListener{
		public void windowCloasing(WindowEvent e){}
		
		public void actionPerformed(ActionEvent e){
			
			int result =  JOptionPane.showConfirmDialog(HomeScreen.frame, 
					"Are You Sure You Want To Exit?",
					"Exit Menu",
					JOptionPane.YES_NO_OPTION,
					JOptionPane.QUESTION_MESSAGE);
			
			if(result == JOptionPane.YES_OPTION){
				accounts.saveAccounts();
				System.exit(0);
			}
		}
	}
	
	class LogInHandler implements ActionListener{
		public void actionPerformed(ActionEvent e){
			LogIn li = new LogIn(accounts, HomeScreen.this);
			li.loginPanel();
		}
	}
	
	class LogOutHandler implements ActionListener{
		public void actionPerformed(ActionEvent e){
			LogOut lo = new LogOut(accounts, HomeScreen.this);
			lo.logoutPanel();
		}
	}
	
	class CreateAccountHandler implements ActionListener{
		public void actionPerformed(ActionEvent e){
			CreateAccount ca = new CreateAccount(accounts, HomeScreen.this);
			ca.createAccountPanel();
		}
	}

}
